package com.diankangwo.dao.impl;

import com.diankangwo.dao.CommodityDAO;
import com.diankangwo.domain.Commodity;
import com.diankangwo.util.PageSupport;
import org.hibernate.Hibernate;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

@Repository
public class CommodityDAOImpl extends BaseDaoImpl<Commodity> implements CommodityDAO {
    @Resource
    private SessionFactory sessionFactory;

    @Override
    public Commodity findCommodity(Integer id) {
        Commodity commodity = (Commodity) sessionFactory.getCurrentSession().get(Commodity.class, id);
        Hibernate.initialize(commodity.getCategories());
        return commodity;
    }

    @Override
    public PageSupport<Commodity> findCommodityByCondition(int pageNo, int pageSize, Integer primary_classification_id, Integer secondary_classification, Integer thridClassificationID, String search_key, String sort_order, Float price_max, Float price_min, Integer globlFieldId, int type) {
        String sql = "from Commodity where isdelete = 0";
        if (globlFieldId != null) {
            sql += " and GlobalFieldId = " + globlFieldId;
        }
        if (primary_classification_id != null) {
            sql += " and typeId = " + primary_classification_id;
        }
        if (secondary_classification != null) {
            sql += " and classificationId = " + secondary_classification;
        }
        if (thridClassificationID != null) {
            sql += " and speciesId = " + thridClassificationID;
        }
        if (search_key != "" && search_key != null) {
            sql += " and commodityName like '%" + search_key + "%'";
        }
        if (price_max != null && price_min != null) {
            sql += " and commodityPriceShop between" + price_min + " and " + price_max;
        }
        if (type == 1) {
            sql += "and (commodityIsNew = 1 or commodityIsVouch = 1 or isHot = 1) ";
        }
        if (sort_order != null && sort_order != "") {
            switch (sort_order) {
                case "commodityIsNew":
                    sql += " order by commodityIsNew ASC";
                    break;
                case "commodityBuyNum":
                    sql += " order by commodityBuyNum ASC";
                    break;
                case "commodityReviews":
                    sql += " order by commodityReviews ASC";
                    break;
                case "commodityPriceShopLow":
                    sql += " order by commodityPriceShop ASC";
                    break;
                case "commodityPriceShopHigh":
                    sql += " order by commodityPriceShop DESC";
                    break;
                case "isHot":
                    sql += " order by isHot ASC";
                    break;
                case "commodityIsVouch":
                    sql += " order by commodityIsVouch ASC";
                    break;
            }
        }else{
            sql += " order by commodityInTime desc";
        }
//        Query query = sessionFactory.getCurrentSession().createQuery(sql);
//        query.list();
        PageSupport<Commodity> list = findPagination(sql, pageNo, pageSize, null);
        return list;
    }

}
